package com.pauldada.sakurabbs.controllers;

import com.pauldada.sakurabbs.dao.CommentRepository;
import com.pauldada.sakurabbs.entities.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("comments")
public class CommentController {

    @Autowired
    CommentRepository commentRepository;

    @GetMapping
    public String DisplayComments(Model model){
        List<Comment> comments = commentRepository.findAll();
        model.addAttribute("comments",comments);
        return "comments/list-comments";
    }

    @GetMapping("/new")
    public String DisplayCommentForm(Model model){
        Comment comment = new Comment();
        model.addAttribute("comment",comment);
        return "comments/new-comment";
    }

    @PostMapping("/save")
    public String CreateComment(Model model,Comment comment){
        commentRepository.save(comment);
        return "redirect:/comments";
    }
}
